package GUI;

import Logica.Appoinment;

import java.awt.Color;
import java.awt.Dimension;

import java.awt.Font;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Cita extends JFrame {
  private JLabel jlCita = new JLabel();
  private JTable jTCitas = new JTable();
  private JSeparator jSeparator1 = new JSeparator();
  private JButton jBAgregarCita = new JButton();
  private JButton jBModificarCita = new JButton();
  private JButton jButtonELIMINAR = new JButton();
  private Object[] info;
  Appoinment app=new Appoinment();
String clinica;
  
  public Cita(String clinicNumber) {
    try {
      clinica = clinicNumber;
      jbInit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.getContentPane().setLayout( null );
    this.setSize(new Dimension(872, 587));
    this.setFont(new Font("ALGERIAN", 0, 25));
    this.setForeground(Color.blue);
    this.setTitle("CITAS ");
    jlCita.setText("CITAS");
    jlCita.setBounds(new Rectangle(330, 5, 95, 80));
    jlCita.setFont(new Font("ALGERIAN", 0, 25));
    jlCita.setForeground(Color.blue);
    jSeparator1.setBounds(new Rectangle(260, 70, 0, 2));
    jBAgregarCita.setText("Agregar");
    jBAgregarCita.setBounds(new Rectangle(450, 475, 90, 35));
    jBAgregarCita.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBAgregarCita_actionPerformed(e);
        }
      });
    jBModificarCita.setText("Modificar");
    jBModificarCita.setBounds(new Rectangle(555, 475, 105, 35));
    jBModificarCita.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBModificarCita_actionPerformed(e);
        }
      });
    jButtonELIMINAR.setText("Eliminar");
    jButtonELIMINAR.setBounds(new Rectangle(675, 475, 90, 40));

    jButtonELIMINAR.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jButtonEliminar_actionPerformed(e);
        }
      });
    Object[][] info = app.DarDatosGuiCita(clinica);
    String[] tit =
    { " APPOINMENT NUMBER", "DATE", "TYPEA", "CLINIC NUMBER", "OWNER NAME",
      "HOUR START", "MIN START", "DURACION","PET ID" };
    DefaultTableModel dtm = new DefaultTableModel(info, tit);

    this.jTCitas = new JTable(info, tit);
    jTCitas.setPreferredScrollableViewportSize(new Dimension(300,
                                                                     100)); // las dimensiones
    jTCitas.setBounds(new Rectangle(10, 95, 840, 350));
    this.getContentPane().add(jButtonELIMINAR, null);
    this.getContentPane().add(jBModificarCita, null);
    this.getContentPane().add(jBAgregarCita, null);
    this.getContentPane().add(jSeparator1, null);
    this.getContentPane().add(jTCitas, null);
    this.getContentPane().add(jlCita, null);


  }

  private void jBAgregarCita_actionPerformed(ActionEvent e) {
  AgregarCita ac=new AgregarCita() ;
  ac.setVisible(true);
  
    }

  private void jBModificarCita_actionPerformed(ActionEvent e) {
    int fila = this.jTCitas.getSelectedRow();

    Object[] info = new Object[9];
    for (int i = 0; i < jTCitas.getColumnCount(); i++) {
      info[i] = jTCitas.getValueAt(fila, i);
    }
    
    ModificarCita mc= new ModificarCita(info) ;
    
    mc.setVisible(true);
  }


  private void jButtonEliminar_actionPerformed(ActionEvent e) {
    int fila = this.jTCitas.getSelectedRow();

    Object[] info = new Object[9];
    for (int i = 0; i < jTCitas.getColumnCount(); i++) {
      info[i] = jTCitas.getValueAt(fila, i);
    }
    System.out.println(info[0]);
    if (app.EliminarCita(String.valueOf(info[0]))) {
      JOptionPane.showMessageDialog(null, "Eliminado",
                                    "La cita ha sido eliminado",
                                    JOptionPane.INFORMATION_MESSAGE);
    } else {
      JOptionPane.showMessageDialog(null, "No Eliminado",
                                    "La cita no ha sido Eliminado",
                                    JOptionPane.ERROR_MESSAGE);
    }


  }
}
